package cn.shujuhai.cms.controller.email;


import cn.shujuhai.cms.domain.EmailRecord;
import cn.shujuhai.cms.service.EmailRecordService;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.pluging.data.ResultVo;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: NiuYaHu
 * @Date: 2024/12/24 14:50
 * @Description:
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/cms/email")
public class EmailController {
    final EmailRecordService emailRecordService;

    /**
     * 查询邮件列表
     *
     * @param subject     邮件主题
     * @param pageRequest
     * @return 查询的邮件列表
     */
    @SysLog(value = "邮件列表查询", operationType = "查询", module = "邮件模块")
    @GetMapping("/list")
    public ResultVo<PageInfo<EmailRecord>> getEmailList
    (String subject, @Valid PageRequest pageRequest) {
        return this.emailRecordService.getEmailPageInfo(subject, pageRequest.getPageNum(), pageRequest.getPageSize());
    }

    /**
     * 邮件新增
     *
     * @param emailRecord 新增邮件对象
     * @return 新增邮件对象
     */
    @SysLog(value = "邮件新增", operationType = "新增", module = "邮件模块")
    @PostMapping("/add")
    public ResultVo<?> save(@RequestBody @Validated EmailRecord emailRecord) {
        return this.emailRecordService.add(emailRecord);
    }

    /**
     * 邮件编辑
     *
     * @param emailRecord 编辑邮件对象
     * @return 更新邮件对象
     */
    @SysLog(value = "邮件编辑", operationType = "编辑", module = "邮件模块")
    @PostMapping("/update")
    public ResultVo<EmailRecord> update(@RequestBody @Validated EmailRecord emailRecord) {
        return this.emailRecordService.edit(emailRecord);
    }

    /**
     * 删除邮件
     *
     * @param id 邮件Id
     * @return 删除响应结果
     */
    @SysLog(value = "删除邮件", operationType = "删除", module = "邮件模块")
    @PostMapping("/delete/{id}")
    public ResultVo<EmailRecord> delete(@PathVariable Integer id) {
        return this.emailRecordService.removeId(id);
    }

    /**
     * 文章附件上传
     *
     * @param file 文件内容
     * @return 结果信息
     */
    @SysLog(value = "邮件附件上传", operationType = "上传文件", module = "邮件模块")
    @PostMapping("/uploadAttachment")
    public ResultVo<?> uploadAttachment(@RequestParam("file") MultipartFile file) {
        return this.emailRecordService.uploadAttachment(file);
    }

    /**
     * 发送邮件
     *
     * @param id 邮件ID
     * @return 结果信息
     */
    @SysLog(value = "发送邮件", operationType = "发送邮件", module = "邮件模块")
    @PostMapping("/sending")
    public ResultVo<?> sending(@RequestParam Long id, @RequestParam String sendTime) {
        return this.emailRecordService.sending(id, sendTime);
    }

    /**
     * 重新发送邮件
     *
     * @param id 邮件ID
     * @return 结果信息
     */
    @SysLog(value = "重新发送邮件", operationType = "发送邮件", module = "邮件模块")
    @PostMapping("/resend")
    public ResultVo<?> resend(@RequestParam Long id, @RequestParam String sendTime) {
        return this.emailRecordService.resend(id, sendTime);
    }

    /**
     * 根据邮件ID获取日志记录
     *
     * @param page     页码
     * @param pageSize 每页大小
     * @param emailId  邮件ID
     * @param status   邮件发送状态
     * @param email    查询条件（邮箱）
     * @return 根据Id查询的邮件日志列表
     */
    @GetMapping("/getEmailLogByEmailId")
    public ResultVo<?> getEmailLogByEmailId(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pageSize,
                                            @RequestParam Long emailId,
                                            @RequestParam Integer status,
                                            @RequestParam(defaultValue = "") String email) {
        return this.emailRecordService.getEmailLogByEmailId(page, pageSize, emailId, status, email);
    }

    /**
     * 根据邮件Id查邮件内容
     *
     * @param id
     * @return 根据Id查询的邮件
     */
    @GetMapping("/getEmailById")
    public ResultVo<?> getEmailById(@RequestParam Long id) {
        return this.emailRecordService.getEmailById(id);
    }
}
